<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 增加浏览次数，默认+1次，字段是 click
 *
 * @param string $table 表的名字，不用加前缀，比如news
 * @param int $id 要增加浏览次数的数据的id值
 * @param int $add 增加浏览的次数，默认是1（每次+1次）
 * @param string $column 增加次数的数据字段，默认是click
 */
function add_click($table,$id,$add=1,$column="click"){
	$CI=&get_instance();
	$CI->load->library('userdata');
	if(!$this->userdata->get($table.$id)){
		$CI->load->database();
		$CI->db->set($column,$column.'+'.intval($add),false);
		$CI->db->update($table,null,array('id'=>$id));
		$this->userdata->set($table.$id,1);
	}
}

function encode_segment($s){
	$CI=&get_instance();
	$CI->load->helper('js');
	$s=js_escape($s);
	return strtr(base64_encode($s),'/','|');
}

function decode_segment($s){
	$CI=&get_instance();
	$CI->load->helper('js');
	$s=strtr($s,'|','/');
	$s=@base64_decode($s);
	return js_unescape($s);
}

function goto_message($message,$uri=-1,$toSiteUrl=true){
	$message=addslashes($message);
	if(is_int($uri))$uri='history.go('.$uri.');';
	else if($toSiteUrl)$uri='location.href="'.site_url($uri).'";';
	header("Content-type:text/html;Charset=utf-8");
	exit('<script>alert("'.$message.'");'.$uri.'</script>');
}

function tag_single($id,$column='content',$strcut=-1){
	static $a=array();
	$id=intval($id);
	if(is_int($column)){
		$strcut=$column;
		$column='content';
	}
	if(!isset($a[$id])){
		$CI=&get_instance();
		$CI->load->database();
		$a[$id]=$CI->db->get_where('single',array('id'=>$id));
		if($a[$id]->num_rows()<1){
			$a[$id]=array();
		}else{
			$a[$id]=$a[$id]->row_array();
		}
	}
	if(isset($a[$id][$column])){
		if($strcut>-1){
			return strcut(strip_tags(nl2br($a[$id][$column])),$strcut);
		}
		return $a[$id][$column];
	}
	return '';
}

function tag_picture($id,$column='photo'){
	static $a=array();
	$id=intval($id);
	if(!isset($a[$id])){
		$CI=&get_instance();
		$CI->load->database();
		$a[$id]=$CI->db->get_where('picture',array('id'=>$id));
		if($a[$id]->num_rows()<1){
			$a[$id]=array();
		}else{
			$a[$id]=$a[$id]->row_array();
		}
	}
	if(isset($a[$id][$column])){
		return $a[$id][$column];
	}
	return '';
}

//获取目录下所有文件
function listDirTree($dirName=null) 
{    
	if(empty($dirName)){    
		exit("directory is empty.");    
	}
	if(is_dir($dirName)){    
		  if($dh=opendir($dirName))    
		  {    
			  $tree=array();   
			  while(($file=readdir($dh))!==false)   
			   {    
				  if($file!="."&&$file!="..")    
				   {
					   $filePath=$dirName."/".$file; 
					   if(is_dir($filePath)) //为目录,递归    
					   {    
						$tree[$file]=listDirTree($filePath);    
					   }    
					   else //为文件,添加到当前数组    
					   {    
						 $tree[]=$file;  
					   }   
				   }   
			  }    
			   closedir($dh);   
		   }    
		   else 
		   {   
			exit("can not open directory $dirName.");
		   }    
		   //返回当前的$tree  
		  return $tree;  
	}    
	else  
	{
		exit("$dirName is not a directory.");   
	}  
} 

function tag_infos($id){
	$CI=&get_instance();
	$CI->load->database();
	$q=$CI->db->get_where('infos',array('id'=>$id));
	$data=$q->row();
	return $data;
}

//获取子栏目列表
function get_kind_list($id){
	static $a=array();
	$id=intval($id);
	if(!isset($a[$id])){
		$CI=&get_instance();
		$CI->load->database();
		$a[$id]=$CI->db->order_by('order_id','asc')->get_where('admin_act',array('parent_id'=>$id,'is_front_show'=>1));
		if($a[$id]->num_rows()<1){
			$a[$id]=array();
		}else{
			$a[$id]=$a[$id]->result();
		}
	}
	return $a[$id];
}


//获取第一个子栏目
function get_first_kind($id){
	static $a=array();
	$id=intval($id);
	if($id!=0){
		if(!isset($a[$id])){
			$CI=&get_instance();
			$CI->load->database();
			$a[$id]=$CI->db->order_by('order_id','asc')->get_where('admin_act',array('parent_id'=>$id,'is_front_show'=>1));
			if($a[$id]->num_rows()<1){
				$a[$id]=array();
			}else{
				$a[$id]=$a[$id]->row();
			}
		}
		if(isset($a[$id]->id)){
			return $a[$id]->id;
		}	
	}
	return '';
}

//获取栏目字段值
function get_kind_column($id,$column='title',$strcut=-1){
	static $a=array();
	$id=intval($id);
	if(is_int($column)){
		$strcut=$column;
		$column='title';
	}
	if(!isset($a[$id])){
		$CI=&get_instance();
		$CI->load->database();
		$a[$id]=$CI->db->get_where('admin_act',array('id'=>$id,'is_front_show'=>1));
		if($a[$id]->num_rows()<1){
			$a[$id]=array();
		}else{
			$a[$id]=$a[$id]->row_array();
		}
	}
	if(isset($a[$id][$column])){
		if($strcut>-1){
			return strcut(strip_tags(nl2br($a[$id][$column])),$strcut);
		}
		return $a[$id][$column];
	}
	return '';
}



//获取path_tree
function get_path_tree($id,$class='',$limit=0){
	$path_link='';
	$path_tree='';
	$path_array='';
	$path_array=get_kind_column($id,'path');
	$path_array=explode(',',$path_array);
	if($limit!=0&&count($path_array)>$limit){
		$path_array=@array_slice($path_array,0,$limit);
	}
	foreach($path_array as $k=>$r){
		if($k==0){
			$path_link=$path_link.$r;
		}else{
			$path_link=$path_link.'/'.$r;
		}
		if($k!=count($path_array)-1){
			$path_tree=$path_tree. '<a href="'.get_kind_column($r,'fronturl').'?'.$path_link.'">'.get_kind_column($r).'</a>&nbsp;>&nbsp;';
		}else{
			$path_tree=$path_tree. '<a href="'.get_kind_column($r,'fronturl').'?'.$path_link.'" class="'.$class.'">'.get_kind_column($r).'</a>';
		}
	}
	return $path_tree;
}

//获取多条数据
/*function get_data_list($table,$where,$top){
	static $array=array();
		$CI=&get_instance();
		$CI->load->database();
		foreach($where as $k=>$r){
			if(is_array($r[$k])){
				//$type_id=implode(',',$r[$k]);	
				$CI->db->or_where_in('type_id',$r[$k]);
				unset($where[array_search($r[$k] , $where)]); //删除该元素
			}
		}
		$array=$CI->db->order_by('sort_id','desc')->get_where($table,$where,$top);
		if($array->num_rows()<1){
			$array=array();
		}else{
			$array=$array->result();
		}
	return $array;
}*/

//获取多条数据
function get_data_list($table,$where,$top){
	static $array=array();
		$CI=&get_instance();
		$CI->load->database();
		$array=$CI->db->order_by('sort_id','desc')->get_where($table,$where,$top);
		if($array->num_rows()<1){
			$array=array();
		}else{
			$array=$array->result();
		}
	return $array;
}

//获取单条数据
function get_data_single($table,$where){
	static $array=array();
		$CI=&get_instance();
		$CI->load->database();
		$array=$CI->db->order_by('sort_id','desc')->get_where($table,$where,1);
		if($array->num_rows()<1){
			$array=array();
		}else{
			$array=$array->row();
		}
	return $array;
}

//获取一二三级id，用在全文搜索功能
function getType_Id($type_id){
   $CI=&get_instance();
   $CI->load->database();
	$where=array('fronturl'=>"news.php");
	$CI->db->order_by("order_id","asc");
	$act=$CI->db->get_where("admin_act",$where)->result();
	foreach ($act as $r){
	 $path=$r->path;
	 $path=explode(",",$path);
	 if(empty($path[0])){$path[0]=0;}
	 if(empty($path[1])){$path[1]=0;}
	 if(empty($path[2])){$path[2]=0;}
	 if($type_id==$path[0] || $type_id==$path[1] ||$type_id==$path[2])
	 {
	 	$aurl="$path[0]/$path[1]/$path[2]";
	 }
	}
	if(empty($aurl)){
	 $aurl="0/0/0";
	}
	 return $aurl;
}
